define([path.deal.html, path.deal.css, 'pingpp', 'icheck'], function (page, css, pingpp, iCheck) {

    var pid;
    var captcha = {
        timer: null,
        isAvaliable: true,
        time: 0
    };
    var buy = {
        num: 0,
        price: 0,
        max: 0,
        deplux: function (num) {
            $('[data-type=buyNum]').val(num);
            $('[data-deplux-num]').text(num);
            $('[data-deplux-price]').text('￥ ' + buy.num * buy.price);
        },
        init: function (price, inventory) {
            buy.price = price;
            buy.max = inventory;
            if (inventory != 0)buy.num = 1;
            else buy.num = 0;
            buy.deplux(buy.num);
        }
    };
    var channel;
    var data={};
    var click = {
        reduceNum: function () {
            if (buy.num == 0)return;
            buy.num--;
            buy.deplux(buy.num)
        },
        addNum: function () {
            if (buy.num >= buy.max)return;
            buy.num++;
            buy.deplux(buy.num)
        },
        back: function () {
            $('[data-step=step2]').hide();
            $('[data-step=step1]').show();
        },
        chooseCannel: function () {
            $('[data-type]').each(function () {
                data[$(this).attr('data-type')] = $(this).val();
            });
            console.dir(data);
            if(data.buyNum<=0)return layer.msg('至少要买一件吧?');
            if(data.name.length<2)return layer.msg('请填写收货人姓名!');
            if(data.tel.length!=11)return layer.msg('请填写手机号!');
            if(data.captcha.length!=6)return layer.msg('请填写验证码!');
            if(data.address.length<6)return layer.msg('请填写较为详细的收货地址!');
            $('[data-step=step1]').hide();
            $('[data-step=step2]').show();
        },
        sendCaptcha: function () {
            if (captcha.isAvaliable == false)return;
            captcha.isAvaliable = true;
            var tel = $('[data-type=tel]').val();

            if (tel.length < 11)return layer.msg('请输入手机号!');

            ex.jsonp({
                url: path.api + 'V3.0.0/Order/verifyPhone',
                method: 'GET',
                data: {
                    phonenum: tel
                },
                success: function (obj) {
                    if (obj.code == 0) {
                        layer.msg('验证码已发送!');
                        captcha.time = 60;
                        $('[data-click=sendCaptcha]').addClass('un').text('剩余60秒');
                        captcha.timer = setInterval(function () {
                            if (captcha.time == 0) {
                                captcha.isAvaliable = false;
                                $('[data-click=sendCaptcha]').removeClass('un');
                                $('[data-click=sendCaptcha]').text('发送验证码');
                            } else {
                                $('[data-click=sendCaptcha]').text('剩余' + captcha.time + '秒');
                                captcha.time--;
                            }
                        }, 1000);
                    } else {
                        captcha.isAvaliable = false;
                    }
                },
                error: function () {
                    layer.msg('您的网络连接不太顺畅哦!');
                    captcha.isAvaliable = false;
                }
            })
        },

        creatOrder: function () {

            data.channel = channel;

            ex.jsonp({
                debug: true,
                url: path.api + 'V3.0.0/Order/info',
                method: 'PUT',
                data: {
                    buyNum: data.buyNum,
                    commodityId: pid,
                    postAddress: data.address,
                    phonenum: data.tel,
                    remark: '',
                    nickname: data.name,
                    captcha: data.captcha,

                    type: 2,
                    pingChannel: data.channel,
                    pingExtra: (function(channel){
                        if(channel=='alipay_wap'){
                            return {
                                success_url: location.origin + location.pathname + '?listId='+query.listId + '#success',
                                cancel_url: location.origin + location.pathname + '?listId='+query.listId + '#cancel'
                            }
                        }else if(channel=='wx_pub'){
                            return {
                                open_id:config.openId
                            }
                        }

                    })(data.channel)
                },
                success: function (obj) {

                    if (obj.code == 0) {
                        cache.charge = obj.data.charge;
                        pingpp.createPayment(cache.charge, function (result, error) {
                            //公众号支付
                            if (result == "success") {
                                location.hash = '#success';
                            }
                            else if (result == "fail") {
                                location.hash = '#cancel';
                            }
                            else if (result == "cancel") {
                                location.hash = '#cancel';
                            }
                        });
                    } else {
                        layer.msg(obj.msg);
                    }
                }
            });

            console.dir(data);
        }
    };

    return {
        init: function (params) {
            pid = params.id;
            if (config.isWX) {
                if (!config.openId)return config.getCode();
            }
            ex.jsonp({
                debug: true,
                url: path.api + 'V2.0.0/Commodity/info',
                method: 'GET',
                data: {
                    _id: pid
                },
                success: function (obj) {

                    if (obj.code == 0) {
                        var data = obj.data.commodityInfo;
                        $('#page').html(juicer(page, {info: data, config: config}));
                        document.title = data.title;

                        buy.init(data.price,+data.inventoryNum);
                        $('[data-step]').hide();

                        $('[data-click]').each(function () {
                            $(this).click(click[$(this).attr('data-click')]);
                        });

                        $('[data-step=step1]').show();

                        $('input[type=radio]').iCheck({
                            checkboxClass: 'icheckbox_square-red',
                            radioClass: 'iradio_square-red',
                            increaseArea: '20%' // optional
                        }).on('ifChecked',function(){
                            channel = $(this).val();
                        });

                        if(config.isWX){
                            $('[value=wx_pub]').iCheck('check');
                        }else{
                            $('[value=alipay_wap]').iCheck('check');
                        }
                    }
                }
            });
        }
    }
});